<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderSnId">
        <el-input v-model="queryParams.orderSnId" placeholder="订单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户ID" prop="userId">
        <el-input v-model="queryParams.userId" placeholder="用户ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="姓名" prop="realname">
        <el-input v-model="queryParams.realname" placeholder="姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="服务类型" prop="serviceNames">
        <el-select v-model="queryParams.serviceNames" placeholder="服务类型" @keyup.enter.native="handleQuery" clearable>
          <el-option v-for="(item, index) in serviceTypesOptions" :key="index" :label="item.typeName"
            :value="item.typeName"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="开票状态" prop="translate">
        <el-select v-model="queryParams.translate" placeholder="开票状态" @keyup.enter.native="handleQuery" clearable>
          <el-option v-for="(item, index) in translateStatusOptions" :key="index" :label="item.name"
            :value="item.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="支付方式" prop="payId">
        <el-select v-model="queryParams.payType" placeholder="支付方式" @keyup.enter.native="handleQuery" clearable>
          <el-option v-for="(item, index) in types" :key="index" :label="item.typeName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="支付状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="支付状态" @keyup.enter.native="handleQuery" clearable>
          <el-option v-for="(item, index) in payManagerStatus" :key="index" :label="item.name"
            :value="item.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="">
        <el-date-picker v-model="time" type="datetimerange" align="right" start-placeholder="开始时间"
          end-placeholder="结束时间" :default-time="['00:00:00', '23:59:59']" value-format="timestamp"></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['pay:manager:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['pay:manager:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['pay:manager:remove']">删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['pay:manager:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="managerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="编号" align="center" prop="id" /> -->
      <el-table-column label="订单编号" align="center" prop="orderSnId" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="姓名" align="center" prop="realname" />
      <el-table-column label="服务类型" align="center" prop="serviceTypes">
        <template slot-scope="scope">
          <p>{{ scope.row.serviceNames
      || '' }}</p>
        </template>
      </el-table-column>
      <el-table-column label="下单时间" align="center" prop="createTime">

        <template slot-scope="scope">
          {{ timestampToDate(scope.row.createTime) }}
        </template>
      </el-table-column>

      <el-table-column label="订单金额" align="center" prop="priceSum" />
      <el-table-column label="开票状态" align="center">

        <template slot-scope="scope">
          <p>{{ translateStatus[scope.row.translate] }}</p>
        </template>
      </el-table-column>

      <el-table-column label="支付方式" align="center">

        <template slot-scope="scope">
          <p>{{ payTypeComputed(scope.row.payType) }}</p>
        </template>
      </el-table-column>

      <el-table-column label="支付状态" align="center" prop="orderStatus">

        <template slot-scope="scope">
          <p>{{ payStatusComputed(scope.row.orderStatus) }}</p>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">

        <template slot-scope="scope">
          <el-button v-if="1 == scope.row.orderStatus" size="mini" type="text" icon="el-icon-edit"
            @click="handleUpdate(scope.row)" v-hasPermi="['pay:manager:changePayment']">变更支付</el-button>
          <span v-else>-</span>
        </template>

      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <changePayMent :showPayStatus="showPayStatus" :visiable="open" disableColumn="orderSnId" :Obj="row" column="orderStatus" :requestURL="requestURL"
      title="变更支付" iptLable="订单编号" :options="orderStatusOptions" @close="close" @confirm=""
      @editOperationStatus="editOperationStatus" :payList="types.filter(item => item.id !==4)" payColmun="payType"></changePayMent>

  </div>
</template>

<script>
import { listManager, getManager, delManager, addManager, updateManager } from "../../../api/finance/payManager";
import { ORDER_STATUS, TRANSLATE_STATUS, ORDER_STATUS_OPTIONS, TRANSLATE_STATUS_OPTIONS, PAY_MANAGER_STATUS } from '../../../constant/const';
import { listPayType } from "../../../api/configManager/payType";
import { listType } from "@/api/service-manager/manager";
import { timestampToDate } from "@/utils/time"
export default {
  name: "Manager",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      showPayStatus: true,
      row: {},
      types: [],
      requestURL: "/pay/manager",
      // 支付管理表格数据
      managerList: [],
      translateStatus: TRANSLATE_STATUS,
      translateStatusOptions: TRANSLATE_STATUS_OPTIONS,
      orderStatus: ORDER_STATUS,
      orderStatusOptions: ORDER_STATUS_OPTIONS,
      payManagerStatus: PAY_MANAGER_STATUS,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderSnId: null,
        userId: null,
        teamId: null,
        priceSum: null,
        orderStatus: null,
        translate: null,
        payType: null,
        payTime: null,
        serviceId: null,
        serviceNames: null,
        params: {
          startTime: null,
          endTime: null
        }
      },
      isSearch: false,
      serviceTypesOptions: [],
      time: []
    };
  },
  created() {
    listPayType(this);
    listType().then(res => {
      this.serviceTypesOptions = res;
    });
  },

  components: {
    changePayMent: () => import("../../../components/payManger/changePayment.vue")
  },

  mounted() {
    this.getList();
  },
  methods: {
    timestampToDate,
    close(value) {
      this.open = value;
    },

    editOperationStatus(e) {
      this.getList()
    },

    /** 查询支付管理列表 */
    getList() {
      this.loading = true;
      let params = {};
      if (this.isSearch) {
        params = Object.assign({}, this.queryParams);
        if (this.time && this.time.length) {
          params.params.startTime = this.time.length ? this.time[0] / 1000 : null;
          params.params.endTime = this.time.length ? this.time[1] / 1000 : null;
        }else{
          delete params.params
        }
      } else {
        params = {
          pageNum: this.queryParams.pageNum,
          pageSize: this.queryParams.pageSize
        }
      }
      listManager(params).then(response => {
        this.managerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderSnId: null,
        userId: null,
        teamId: null,
        priceSum: null,
        orderStatus: null,
        translate: null,
        payType: null,
        createTime: null,
        updateTime: null,
        payTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.isSearch = true;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.isSearch = false;
      this.resetForm("queryForm");
      this.time = [];
      this.queryParams.payType = undefined;
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加支付管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.row = row;

      this.showPayStatus = row.showPayStatus == 1;

      this.open = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateManager(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addManager(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除支付管理编号为"' + ids + '"的数据项？').then(function () {
        return delManager(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pay/manager/export', {
        ...this.queryParams,
        ids: this.ids.length ? this.ids : undefined
      }, `manager_${new Date().getTime()}.xlsx`)
    }
  },
  computed: {
    payTypeComputed() {
      return (item) => {
        let obj = this.types.find(ite => ite.id == item);
        let type = obj ? obj.typeName : "";
        return type;
      }
    },
    payStatusComputed() {
      return (e) => {
        if (e === '' || e === null) return "";
        if (e == 0) {
          return "待支付"
        } else if (e == 1) {
          return "待确认"
        } else if ([2, 3, 4, 5].includes(e)) {
          return "已支付"
        } else if (-1 == e) {
          return "取消支付"
        } else {
          return "已支付"
        }
      }
    }
  }
};
</script>
